package com.project.utils;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;


import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * ClassName: SecurityUtil
 * Package: com.project.utils
 * Description:
 *
 * @Author 看到风
 * @Create 2024/10/22 15:17
 * @Version 1.0
 */
@Slf4j
public class SecurityUtil {
    public static XcUser getUser() {
        try {
            Object principalObj =
                    SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            System.out.println(principalObj);
            if (principalObj instanceof String) {
            //取出用户身份信息
                String principal = principalObj.toString();
            //将 json 转成对象
                XcUser user = JSON.parseObject(principal,
                        XcUser.class);
                return user;
            }
        } catch (Exception e) {
            log.error("获取当前登录用户身份出错:{}",
                    e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
    @Data
    public static class XcUser implements Serializable {
        private static final long serialVersionUID = 1L;
        //id
        private Long id;

        //用户身份 0：学生  老师身份  1:普通老师  2：院系、校系老师
        private  Integer identity;

        //院系
        private String department;
        //专业
        private String major;

        //用户学号
        private Long userId;

        //用户姓名
        private String name;

        //如果是老师 填职位
        private String position;

        //电话
        private String phone;

        //用户密码
        private String password;

        //创建者的学号
        // @TableField(fill = FieldFill.INSERT)//插入时填充字段
        private Long createUserId;

        //修改者的学号
        //   @TableField(fill = FieldFill.INSERT_UPDATE)//插入和更新时填充字段
        private Long modifiedUserId;

        //创建时间
        //   @TableField(fill = FieldFill.INSERT)//插入时填充字段
        private LocalDateTime createTime;

        //修改时间
//    @TableField(fill = FieldFill.INSERT_UPDATE)//插入和更新时填充字段
        private LocalDateTime modifiedTime;

        //逻辑删除字段

        private Integer deleted;
    }
}
